/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package com.aionengine.gameserver.taskmanager.tasks;

import com.aionengine.gameserver.model.gameobjects.Item;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.network.aion.serverpackets.SM_SYSTEM_MESSAGE;
import com.aionengine.gameserver.taskmanager.AbstractPeriodicTaskManager;
import com.aionengine.gameserver.utils.PacketSendUtility;
import com.aionengine.gameserver.world.World;
import javolution.util.FastMap;

import java.util.Collection;
import java.util.Map;

/**
 * @author Mr. Poke
 */
public class TemporaryTradeTimeTask extends AbstractPeriodicTaskManager {

    private final FastMap<Item, Collection<Integer>> items = new FastMap<Item, Collection<Integer>>();
    private final FastMap<Integer, Item> itemById = new FastMap<Integer, Item>();

    /**
     * @param period
     */
    public TemporaryTradeTimeTask() {
        super(1000);
    }

    public static TemporaryTradeTimeTask getInstance() {
        return SingletonHolder._instance;
    }

    public void addTask(Item item, Collection<Integer> players) {
        writeLock();
        try {
            items.put(item, players);
            itemById.put(item.getObjectId(), item);
        } finally {
            writeUnlock();
        }
    }

    public boolean canTrade(Item item, int playerObjectId) {
        Collection<Integer> players = items.get(item);
        if (players == null)
            return false;
        return players.contains(playerObjectId);
    }

    public boolean hasItem(Item item) {
        readLock();
        try {
            return items.containsKey(item);
        } finally {
            readUnlock();
        }
    }

    public Item getItem(int objectId) {
        readLock();
        try {
            return itemById.get(objectId);
        } finally {
            readUnlock();
        }
    }

    @Override
    public void run() {
        writeLock();
        try {
            for (Map.Entry<Item, Collection<Integer>> entry : items.entrySet()) {
                Item item = entry.getKey();
                int time = (item.getTemporaryExchangeTime() - (int) (System.currentTimeMillis() / 1000));
                if (time == 60) {
                    for (int playerId : entry.getValue()) {
                        Player player = World.getInstance().findPlayer(playerId);
                        if (player != null)
                            PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.STR_MSG_END_OF_EXCHANGE_TIME(item.getNameId(), time));
                    }
                } else if (time <= 0) {
                    for (int playerId : entry.getValue()) {
                        Player player = World.getInstance().findPlayer(playerId);
                        if (player != null)
                            PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.STR_MSG_EXCHANGE_TIME_OVER(item.getNameId()));
                    }
                    item.setTemporaryExchangeTime(0);
                    items.remove(item);
                    itemById.remove(item.getObjectId());
                }
            }
        } finally {
            writeUnlock();
        }
    }

    @SuppressWarnings("synthetic-access")
    private static class SingletonHolder {

        protected static final TemporaryTradeTimeTask _instance = new TemporaryTradeTimeTask();
    }
}
